<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo/css/main.css">


<link rel="stylesheet" href="/hexo/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"youchen12138.gitee.io","root":"/hexo/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="JavaScript中有六种类型 String 字符串  Number 数值  Boolean 布尔值  Null 空值  Undefined 未定义  Object 对象    强制类型转换只能在String，Number，Boolean三者之间转换  数组与字符串互转在开发中经常需要把字符串与数组互转 Array对象中的join()方法 该方法可以将数组转换为多种形式的字符串  1234var">
<meta property="og:type" content="article">
<meta property="og:title" content="类型强制转换">
<meta property="og:url" content="https://youchen12138.gitee.io/hexo/2019/04/07/%E7%B1%BB%E5%9E%8B%E5%BC%BA%E5%88%B6%E8%BD%AC%E6%8D%A2/index.html">
<meta property="og:site_name" content="幽辰的博客">
<meta property="og:description" content="JavaScript中有六种类型 String 字符串  Number 数值  Boolean 布尔值  Null 空值  Undefined 未定义  Object 对象    强制类型转换只能在String，Number，Boolean三者之间转换  数组与字符串互转在开发中经常需要把字符串与数组互转 Array对象中的join()方法 该方法可以将数组转换为多种形式的字符串  1234var">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2019-04-07T11:32:25.000Z">
<meta property="article:modified_time" content="2020-08-25T16:05:26.095Z">
<meta property="article:author" content="幽辰">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://youchen12138.gitee.io/hexo/2019/04/07/%E7%B1%BB%E5%9E%8B%E5%BC%BA%E5%88%B6%E8%BD%AC%E6%8D%A2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>类型强制转换 | 幽辰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">幽辰的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/hexo/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/hexo/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/hexo/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/hexo/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives<span class="badge">127</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2019/04/07/%E7%B1%BB%E5%9E%8B%E5%BC%BA%E5%88%B6%E8%BD%AC%E6%8D%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          类型强制转换
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2019-04-07 19:32:25" itemprop="dateCreated datePublished" datetime="2019-04-07T19:32:25+08:00">2019-04-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-08-26 00:05:26" itemprop="dateModified" datetime="2020-08-26T00:05:26+08:00">2020-08-26</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="JavaScript中有六种类型"><a href="#JavaScript中有六种类型" class="headerlink" title="JavaScript中有六种类型"></a>JavaScript中有六种类型</h2><ul>
<li><p>String 字符串</p>
</li>
<li><p>Number 数值</p>
</li>
<li><p>Boolean 布尔值</p>
</li>
<li><p>Null 空值</p>
</li>
<li><p>Undefined 未定义</p>
</li>
<li><p>Object 对象</p>
</li>
</ul>
<blockquote>
<p>强制类型转换只能在String，Number，Boolean三者之间转换</p>
</blockquote>
<h2 id="数组与字符串互转"><a href="#数组与字符串互转" class="headerlink" title="数组与字符串互转"></a>数组与字符串互转</h2><p>在开发中经常需要把字符串与数组互转</p>
<h3 id="Array对象中的join-方法"><a href="#Array对象中的join-方法" class="headerlink" title="Array对象中的join()方法"></a>Array对象中的join()方法</h3><blockquote>
<p>该方法可以将数组转换为多种形式的字符串</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var a &#x3D; [1, 2, 3, 4, 5];</span><br><span class="line">a &#x3D; a.join(&quot;-&quot;);</span><br><span class="line">document.write(&quot;a类型 &#x3D; &quot; + typeof(a) + &quot;&lt;br&#x2F;&gt;&quot;)</span><br><span class="line">document.write(&quot;a的值 &#x3D; &quot; + a)</span><br></pre></td></tr></table></figure>
<p>结果为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a类型 &#x3D; string</span><br><span class="line">a的值 &#x3D; 1-2-3-4-5</span><br></pre></td></tr></table></figure>
<p>join()方法包含一个参数，用于定义合并元素的连字符，若不提供参数，则以逗号连接每个元素</p>
<h3 id="Array对象中的spilt-方法"><a href="#Array对象中的spilt-方法" class="headerlink" title="Array对象中的spilt()方法"></a>Array对象中的spilt()方法</h3><blockquote>
<p>该方法可以将字符串转换为数组</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var a &#x3D; [1, 2, 3, 4, 5];</span><br><span class="line">a &#x3D; a.join(&quot;-&quot;);</span><br><span class="line">var s &#x3D; a.spilt(&quot;-&quot;);</span><br><span class="line">document.write(&quot;a类型 &#x3D; &quot; + typeof(a) + &quot;&lt;br&#x2F;&gt;&quot;)</span><br><span class="line">document.write(&quot;a的值 &#x3D; &quot; + a)</span><br></pre></td></tr></table></figure>
<p>结果为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a类型 &#x3D; object</span><br><span class="line">a的值 &#x3D; 1-2-3-4-5</span><br></pre></td></tr></table></figure>

<h2 id="其他类型转换为字符串"><a href="#其他类型转换为字符串" class="headerlink" title="其他类型转换为字符串"></a>其他类型转换为字符串</h2><h3 id="toString-方法"><a href="#toString-方法" class="headerlink" title="toString()方法"></a>toString()方法</h3><blockquote>
<p>返回值为String类型</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var a &#x3D; 123;</span><br><span class="line">a &#x3D; a.tostring();</span><br><span class="line">document.write(a);</span><br></pre></td></tr></table></figure>
<p>结果为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">String</span><br></pre></td></tr></table></figure>
<blockquote>
<p>Boolean和上面相同，toString()方法可以将Boolean转换为String类型</p>
</blockquote>
<blockquote>
<p>注：不能将Null和Undefined类型转换为String类型</p>
</blockquote>
<h2 id="字符串转换为数值"><a href="#字符串转换为数值" class="headerlink" title="字符串转换为数值"></a>字符串转换为数值</h2><h3 id="parseInt-方法"><a href="#parseInt-方法" class="headerlink" title="parseInt()方法"></a>parseInt()方法</h3><blockquote>
<p>将一个字符串转换为一个整数</p>
</blockquote>
<p>可以将字符串中的有效数值提取出来，比如示例，1是数值，继续；2是数值，继续；3是数值，继续；p不是数值，结束。</p>
<p>若a=123px45，强制转换类型之后的结果为123.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var a &#x3D; &quot;123px&quot;;</span><br><span class="line">a &#x3D; parseInt(a);</span><br><span class="line">&#x2F;&#x2F;从String转换为Number，结果为123</span><br></pre></td></tr></table></figure>

<h3 id="parseFloat-方法"><a href="#parseFloat-方法" class="headerlink" title="parseFloat()方法"></a>parseFloat()方法</h3><blockquote>
<p>将一个字符串转换为一个浮点数</p>
</blockquote>
<p>功能和parseInt相似，只是读取有效数值为浮点数而已。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var a &#x3D; &quot;123.456px&quot;;</span><br><span class="line">a &#x3D; parseFloat(a);</span><br><span class="line">&#x2F;&#x2F;从String转换为Number，结果为123.456</span><br></pre></td></tr></table></figure>

<blockquote>
<p>如果对非String使用parseInt()或parseFloat()，它会将其转换为String然后再操作。</p>
</blockquote>
<h2 id="通用方法"><a href="#通用方法" class="headerlink" title="通用方法"></a>通用方法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var a &#x3D; 123;</span><br><span class="line">a &#x3D; String(a);&#x2F;&#x2F;其他的类型也可以这样转换</span><br></pre></td></tr></table></figure>
<blockquote>
<p>空字符串转换为数值时为0，有非数字的字符串转换为数值时为NaN</p>
</blockquote>
<blockquote>
<p>true布尔值转换为数值为1，false布尔值转换为数值为0</p>
</blockquote>
<blockquote>
<p>Undefined转换为数值时为NaN</p>
</blockquote>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/hexo/tags/JavaScript/" rel="tag"># JavaScript</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/hexo/2019/04/07/JS%E7%BC%96%E5%86%99%E4%BD%8D%E7%BD%AE/" rel="prev" title="JS编写位置">
      <i class="fa fa-chevron-left"></i> JS编写位置
    </a></div>
      <div class="post-nav-item">
    <a href="/hexo/2019/04/09/%E8%BF%90%E7%AE%97%E7%AC%A6/" rel="next" title="运算符">
      运算符 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#JavaScript%E4%B8%AD%E6%9C%89%E5%85%AD%E7%A7%8D%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.</span> <span class="nav-text">JavaScript中有六种类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BA%92%E8%BD%AC"><span class="nav-number">2.</span> <span class="nav-text">数组与字符串互转</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Array%E5%AF%B9%E8%B1%A1%E4%B8%AD%E7%9A%84join-%E6%96%B9%E6%B3%95"><span class="nav-number">2.1.</span> <span class="nav-text">Array对象中的join()方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Array%E5%AF%B9%E8%B1%A1%E4%B8%AD%E7%9A%84spilt-%E6%96%B9%E6%B3%95"><span class="nav-number">2.2.</span> <span class="nav-text">Array对象中的spilt()方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">3.</span> <span class="nav-text">其他类型转换为字符串</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#toString-%E6%96%B9%E6%B3%95"><span class="nav-number">3.1.</span> <span class="nav-text">toString()方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B0%E5%80%BC"><span class="nav-number">4.</span> <span class="nav-text">字符串转换为数值</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#parseInt-%E6%96%B9%E6%B3%95"><span class="nav-number">4.1.</span> <span class="nav-text">parseInt()方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#parseFloat-%E6%96%B9%E6%B3%95"><span class="nav-number">4.2.</span> <span class="nav-text">parseFloat()方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%9A%E7%94%A8%E6%96%B9%E6%B3%95"><span class="nav-number">5.</span> <span class="nav-text">通用方法</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="幽辰"
      src="/hexo/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">幽辰</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/hexo/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/hexo/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/hexo/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">幽辰</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/hexo/lib/anime.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.ui.min.js"></script>

<script src="/hexo/js/utils.js"></script>

<script src="/hexo/js/motion.js"></script>


<script src="/hexo/js/schemes/pisces.js"></script>


<script src="/hexo/js/next-boot.js"></script>




  















  

  

</body>
</html>
